package se480.paf.filters.impl;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import org.apache.log4j.Logger;

import se480.paf.filters.ActiveFilter;
import se480.paf.pipes.Item;
import se480.paf.pipes.Pipe;
import se480.paf.pipes.impl.Queue;


/**
 * This filter is at the front of the system.
 * 
 * Output:  List<String> representing file lines
 * 
 * @author mmichalak
 *
 */
public class FileReaderFilter implements ActiveFilter {
	
	static Logger logger = Logger.getLogger("FileReaderFilter");
	
	String name = "FileReader";
	int msSleep = 2000; // Default Thread sleep time
	
	public Pipe<Item> inPipe;
	public Pipe<Item> outPipe;


	
	@Override
	public void run() {
		try {

		      // Loop over files and dir and read-in.  In a real prod job, this would 
		      // read the files and then delete or move this.  This just does one read
		      // and ends the thread
		      int i = 0;
		      while (true) {
		    		 process(null); // Should implement a Null Object pattern for Item here
			    	  
			    	 logger.debug(name + " wake: " + i++);
				     Thread.sleep(msSleep);
					/*
					 * REMOVE break.  Note this break is temp.  IF this were a real system,
					 * each file would be moved after being processed.  However; that is 
					 * not done for this system.
					 */					
					 break;  
		    	 }
	      } catch (InterruptedException e) {
	         //logger.debug(name + " interrupted.");
	      }
	   logger.debug(name + " exiting.");
	}

	@Override
	public void process(Item item) {
		long startTime = System.currentTimeMillis();
        ArrayList<String> lines = new ArrayList<String>();

        try {
	        String sCurrentLine;
	 
	        //String fileName = "c:\\SE480\\DataFiles\\Text1.txt";
	        File path = new File("c:\\SE480\\DataFiles\\");
	        /*FileReader r = new FileReader(
					FileReadTest.class.getClassLoader().getResource(fileName).getPath()
			        .replaceAll("%20", " "));*/
	        File files[] = path.listFiles();
	        
	        for (int i = 0, n = files.length; i < n; i++) {
	            //System.out.println(files[i].toString());
	        	lines = new ArrayList<String>();
	            if (!files[i].isDirectory()) {	      
	            	if (files[i].getName().toLowerCase().contains("stopwords")) continue;  // stopwords file.  Skip

			        FileReader r = new FileReader(files[i]);
			        BufferedReader br = 
			            //new BufferedReader(new FileReader("C:\\eclipse-helios sandbox\\Se435Udp\\bin\\DataInput.txt"));
			        	new BufferedReader(r);
			        
			        int N = 0;
			        while ((sCurrentLine = br.readLine()) != null) {
			        	lines.add(sCurrentLine);
			        	N++;
				       //logger.debug(sCurrentLine);
			        } 	 			   			        
			        
				    item = new Item(lines);
				    item.setName(files[i].getName());
				    long endTime = System.currentTimeMillis();
				    item.addExecuteTimes(name + " - " + (endTime - startTime));
				    
				    outPipe.addItem(item);
			    
		            }
	        }
		} catch (FileNotFoundException e) {
	        e.printStackTrace();
	    } catch (IOException e) {
	        e.printStackTrace();
	    }


	        
	}


	@Override
	public void setInPipe(Pipe<Item> pipe) {
		this.inPipe = pipe;
	}

	@Override
	public void setOutPipe(Pipe<Item> pipe) {
		this.outPipe = pipe;
	}

	@Override
	public Pipe<Item> getOutPipe() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String getName() {
		return this.name;
	}

	@Override
	public void setSleep(int msSleep) {
		this.msSleep = msSleep;
	}

	@Override
	public int getSleep() {
		return this.msSleep;
	}

	@Override
	public Pipe<Item> getInPipe() {
		// TODO Auto-generated method stub
		return null;
	}

}
